//
//  ChooseAgeView.m
//  Cycling
//
//  Created by 迟国强 on 2023/3/7.
//

#import "ChooseAgeView.h"

@implementation ChooseAgeView

+ (ChooseAgeView *)chooseAgeWith:(UIViewController *)vc Complection:(void(^)(NSString *age))block {
    ChooseAgeView *  inputView;
    inputView = [[ChooseAgeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 56, 373)];
    [inputView initUI];
    inputView.complectionBlock = block;
    inputView.dataArr = @[@"40以下",@"40s",@"50s",@"60s",@"70s",@"70以上"];
    NSLog(@"%@", NSObject.getKeyWindowRootViewController);
    [NSObject.getKeyWindowRootViewController gq_presentLikeAlertView:inputView];
    NSObject.getKeyWindowRootViewController.gq_enableBlankDismiss = NO;
    
    return inputView;
}

- (void)sure{
    NSInteger row = [self.datePicker selectedRowInComponent:0];
    NSString *age = self.dataArr[row];
    if ([age isEqualToString:@"40以下"]) {
        self.age = @"40-";
    } else if ([age isEqualToString:@"70以上"]) {
        self.age = @"70+";
    } else {
        self.age = [age stringByReplacingOccurrencesOfString:@"s" withString:@""];
    }
    self.complectionBlock(self.age);
}
- (void)cancle{
    self.complectionBlock(@"");
}

- (void)initUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 56, 373)];
    view.userInteractionEnabled = YES;
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 16;
    view.layer.masksToBounds = YES;
    view.center = self.center;
    [self addSubview:view];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    cancleBtn.userInteractionEnabled = YES;
    [view addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.mas_right);
        make.top.equalTo(view.mas_top).offset(5);
        make.width.offset(58);
        make.height.offset(58);
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = @"请选择您的年龄";
    titleL.textColor = COLOR_TOPIC_SELECT;
    titleL.font = [UIFont systemFontOfSize:26];
    titleL.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top).offset(43);
            make.centerX.equalTo(self.mas_centerX);
    }];
        
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(29);
        make.right.equalTo(view.mas_right).offset(-29);
        make.bottom.equalTo(view.mas_bottom).offset(-28);
        make.height.offset(54);
    }];
    
    self.datePicker=[[UIPickerView alloc]init];
//    if (@available(iOS 13.4, *)) {
//        self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
//    }
    //frame得在设置样式之后设置,否者宽度位置显示会有问题
    [self.datePicker setFrame:CGRectMake(30, 100, view.ce_width - 60, 180)];
    self.datePicker.delegate = self;
    self.datePicker.dataSource = self;
    
//    [self.datePicker setDatePickerMode:UIDatePickerModeDate];
//    [self datePickerSetUp];
    [self addSubview:self.datePicker];

}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArr.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataArr[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *age = self.dataArr[row];
    if ([age isEqualToString:@"40以下"]) {
        self.age = @"40-";
    } else if ([age isEqualToString:@"70以上"]) {
        self.age = @"70+";
    } else {
        self.age = [age stringByReplacingOccurrencesOfString:@"s" withString:@""];
    }
}


@end
